package com.biao.synchronized_test;

public class Test06_join {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> System.out.println(Thread.currentThread().getName() + "---t1"));
//        Thread t2 = new Thread(() -> System.out.println(Thread.currentThread().getName() + "---t2"));
//        Thread t3 = new Thread(() -> System.out.println(Thread.currentThread().getName() + "---t3"));
//        t1.start();
//        t2.start();
//        t3.start();
        // 以上写法不一定会按顺序输出，程序是乱序执行的，cpu不一定切换到哪个线程

        // 使用join() 实现顺序输出
        Thread t2 = new Thread(() -> {
            try {
                t1.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "---t2");
        });
        Thread t3 = new Thread(() -> {
            try {
                t2.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "---t3");
        });
        t1.start();
        t2.start();
        t3.start();
        // join 使用wait实现的，join的线程执行完后会把所有线程都给唤醒然后重新分配资源
    }
}
